/**
 * Download according to the background interface file stream
 * @param {*} data
 * @param {*} filename
 * @param {*} mime
 * @param {*} bom
 */
export function downloadByData(data: BlobPart, filename: string, mime?: string, bom?: BlobPart) {
	const blobData = typeof bom !== "undefined" ? [bom, data] : [data];
	const blob = new Blob(blobData, { type: mime || "application/octet-stream" });

	const blobURL = window.URL.createObjectURL(blob);
	const tempLink = document.createElement("a");
	tempLink.style.display = "none";
	tempLink.href = blobURL;
	tempLink.setAttribute("download", filename);
	if (typeof tempLink.download === "undefined") tempLink.setAttribute("target", "_blank");

	document.body.appendChild(tempLink);
	tempLink.click();
	document.body.removeChild(tempLink);
	window.URL.revokeObjectURL(blobURL);
}

/**
 * Download file according to file address
 * @param {*} sUrl
 */
export function downloadByUrl({
	url,
	target = "_blank",
	fileName
}: {
	url: string;
	target?: TargetContext;
	fileName?: string;
}): boolean {
	const isChrome = window.navigator.userAgent.toLowerCase().includes("chrome");
	const isSafari = window.navigator.userAgent.toLowerCase().includes("safari");

	if (/(iP)/g.test(window.navigator.userAgent)) {
		console.error("Your browser does not support download!");
		return false;
	}
	if (isChrome || isSafari) {
		const link = document.createElement("a");
		link.href = url;
		link.target = target;

		if (link.download !== undefined) link.download = fileName || url.substring(url.lastIndexOf("/") + 1, url.length);

		if (document.createEvent) {
			const e = document.createEvent("MouseEvents");
			e.initEvent("click", true, true);
			link.dispatchEvent(e);
			return true;
		}
	}
	if (!url.includes("?")) url += "?download";

	openWindow(url, { target });
	return true;
}
function openWindow(url: string, opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }) {
	const { target = "__blank", noopener = true, noreferrer = true } = opt || {};
	const feature: string[] = [];

	noopener && feature.push("noopener=yes");
	noreferrer && feature.push("noreferrer=yes");

	window.open(url, target, feature.join(","));
}
